import { action } from "mobx";
import * as api from "../api/api";
import request from "../utils/request";
import { LoadHoc, loading } from '../hoc/loadHoc';

interface resRequest {
  totalCount: number,
  items: api.ProductsOutput[]
}

export default class Fish extends LoadHoc {
  // 购买期数列表
  @loading @action async getProductsAsync(params: {
    pageIndex: number,
    pageSize: number,
    callback?: (res: resRequest) => void;
  }) {
    const { pageIndex, pageSize, callback } = params;
    const res = await request(api.ProductServiceApiFetchParamCreactor.getProductsAsync({
      form: {
        skipCount: (pageIndex - 1) * pageSize,
        maxResultCount: pageSize
      }
    }));
    callback && callback(res)
  }

  // 获取联养详情
  @loading @action async productDetails(params: {
    form: { id: string },
    callback: (res: api.ProductOutput) => void
  }) {
    const { form, callback } = params
    const res = await request(api.ProductServiceApiFetchParamCreactor.productDetails({
      form
    }));
    callback && callback(res)
  }

  // 获取联养等级
  @loading @action async gradeLists(params: {
    callback: (res: api.GradeListOutput[]) => void
  }) {
    const { callback } = params
    const res = await request(api.ProductServiceApiFetchParamCreactor.gradeLists());
    callback && callback(res || [])
  }

  // 下单接口
  @loading @action async placeAnOrder(params: {
    id: string,
    gradeId: string,
    cultureCycleId: string,
    callback?: (res: api.OrderOutput) => void;
  }) {
    const { id, gradeId, cultureCycleId, callback } = params;
    const res = await request(api.OrderApiFetchParamCreactor.placeAnOrder({
      form: {
        id,
        gradeId,
        cultureCycleId
      }
    }));
    callback && callback(res)
  }

  // 购买记录列表
  @loading @action async userOrderRecordPage(params: {
    productId: string,
    pageIndex: number,
    pageSize: number,
    callback?: (res: resRequest) => void;
  }) {
    const { productId, pageIndex, pageSize, callback } = params;
    const res = await request(api.OrderApiFetchParamCreactor.userOrderRecordPage({
      form: {
        productId,
        skipCount: (pageIndex - 1) * pageSize,
        maxResultCount: pageSize
      }
    }));
    callback && callback(res)
  }

  // 鱼塘/订单
  @loading @action async myRecordPage(params: {
    type: number,
    pageIndex: number,
    pageSize: number,
    callback?: (res: resRequest) => void;
  }) {
    const { type, pageIndex, pageSize, callback } = params;
    const res = await request(api.OrderApiFetchParamCreactor.myRecordPage({
      form: {
        type,
        skipCount: (pageIndex - 1) * pageSize,
        maxResultCount: pageSize
      }
    }));
    callback && callback(res)
  }

  // 订单详情
  @loading @action async orderDetailsById(params: {
    id: string,
    callback?: (res: api.EarningsOutput) => void;
  }) {
    const { id, callback } = params;
    const res = await request(api.OrderApiFetchParamCreactor.orderDetailsById({
      form: {
        id
      }
    }));
    callback && callback(res)
  }
}
